lizards <- read_csv(here("data_tidy", "lizards.csv"))
## Rows: 1628 Columns: 16
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (10): date, scientific_name, common_name, zone, site, plot, spp, sex, rc...
## dbl (6): pit, toe_num, sv_length, total_length, weight, pc
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Histogram (really bad) of lizard weights
ggplot(data = lizards, aes(x = weight)) +
geom_histogram(fill = "orange",
color = "red",
size = 0.2,
linetype = "dotted")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Scatterplot
ggplot(data = lizards, aes(x = total_length, y = weight)) +
geom_point(shape = 22,
color = "green",
fill = "purple",
size = 4,
alpha = 0.5)
Bubble plot where the color of the points changes based on common_name and the size of points varies based on total_length.
DISCLAIMER: BAD IDEA!
ggplot(data = lizards, aes(x = total_length, y = weight)) +
geom_point(aes(color = common_name, size = total_length),
shape = 22,
fill = "black") +
theme_minimal()
ggplot(data = lizards, aes(x = total_length, y = weight)) +
geom_point(aes(color = common_name)) +
theme_light() +
facet_wrap(~common_name, ncol = 4)
ggplot(data = lizards, aes(x = total_length, y = weight)) +
geom_point(aes(color = common_name)) +
theme_light() +
facet_grid(sex ~ tail)
Find total lizard counts by common name
# This way would make more sense if you're doing more summary variables than just count. Then you could put mean, sd, etc in with n() within the summarize function.
lizard_counts <- lizards %>%
group_by(common_name) %>%
summarize(count = n())
# does the same thing in this case.
lizard_counts <- lizards %>% dplyr::count(common_name)
lizard_counts
## # A tibble: 7 × 2
## common_name n
## <chr> <int>
## 1 colorado checkered 31
## 2 eastern fence 29
## 3 lesser earless 37
## 4 little striped 160
## 5 side-blotched lizard 1176
## 6 texas horned 19
## 7 western whiptail 176
lizard_counts_cn_tail <- lizards %>% count(common_name, tail)
lizard_counts_cn_tail
## # A tibble: 13 × 3
## common_name tail n
## <chr> <chr> <int>
## 1 colorado checkered b 2
## 2 colorado checkered w 29
## 3 eastern fence b 4
## 4 eastern fence w 25
## 5 lesser earless b 1
## 6 lesser earless w 36
## 7 little striped b 34
## 8 little striped w 126
## 9 side-blotched lizard b 277
## 10 side-blotched lizard w 899
## 11 texas horned w 19
## 12 western whiptail b 26
## 13 western whiptail w 150
# ggplot(data = lizard_counts, aes(x = common_name, y = n)) +
# geom_col() +
#coord_flip()
# fct_reorder() creates a factor and has it ordered by variable specified
ggplot(data = lizard_counts, aes(y = fct_reorder(common_name, n),
x = n)) +
geom_col(aes(fill = common_name), show.legend = FALSE) +
labs(x = "Lizard Counts", y = "Common Name")
Try converting common name to an ordered factor outside of ggplot.
lizard_counts = lizard_counts %>%
mutate(common_name = fct_reorder(common_name, n))
Scatterplot: total_length (x) vs. weight (y)
ggplot(data = lizards, aes(x = total_length, y = weight)) +
geom_point() +
scale_x_continuous(breaks = c(0, 10, 50, 500),
limits = c(c(0, 500)),
expand = c(0,0)) +
scale_y_continuous(expand = c(0,0))
Transform the date column to class Date, then find counts of observations by date.
lizard_counts <- lizards %>%
mutate(date = lubridate::mdy(date)) %>%
count(date)
Make a line plot (geom_line()) of date (x) and count (y)
ggplot(data = lizard_counts, aes(x = date, y = n)) +
geom_line() +
scale_x_date(date_breaks = "3 years",
date_labels = "%Y") #%Y specifies 4 digit year, %y would do 2 digit year, etc.
ggplot(data = lizards, aes(x = total_length, y = weight)) +
geom_point(aes(color = weight)) +
scale_color_stepsn(colors = c("green", "blue", "purple"),
breaks = c(0, 20, 40, 50, 60))
Make a horizontal boxplot with common_name on the y-axis, total_length on the x-axis, with color changing based on common_name.
lizards_fct <- lizards %>%
mutate(common_name = fct_reorder(common_name, total_length, .fun = median))
ggplot(data = lizards_fct, aes(y = common_name, x = total_length)) +
geom_boxplot(aes(fill = common_name), show.legend = FALSE) +
scale_fill_paletteer_d(palette = "calecopal::sage")
ggplot(data = lizards, aes(x = total_length, y = weight)) +
geom_point() +
theme(
panel.background = element_rect(fill = "yellow",
color = "purple",
size = 10),
# panel.grid.major.x = element_line(color = "red"),
# panel.grid.minor.x = element_line(color = "green"),
# panel.grid.major = element_line(color = "blue"),
# panel.grid.minor.y = element_line(color = "magenta")
panel.grid = element_blank(), # gets rid of gridlines.
axis.text.x = element_text(color = "orange"),
axis.title = element_text(color = "green", size = 15)
)
add labels to a plot
Make a subset from lizards, called ww_lizards, that only contains observations for “western whiptail” lizards from the site “sand”
ww_lizards <- lizards %>%
filter(common_name == "western whiptail", site == "sand")
ggplot(data = ww_lizards, aes(y = total_length, x = weight)) +
geom_point() +
geom_text_repel(aes(label = toe_num),
size = 3,
max.overlaps = 20)
Filter for only Europe. Create a scatterplot of gdpPercap (x) and lifeExp (y), labeled by the country name.
# View(gapminder)
eur_gapminder <- gapminder %>%
filter(continent == "Europe", year > 2000)
ggplot(data = eur_gapminder, aes(x = gdpPercap, y = lifeExp)) +
geom_point() +
geom_text_repel(aes(label = country),
size = 2.5
)
p <- ggplot(lizards, aes(x = total_length, y = weight)) +
geom_point()
p + theme_light()
p + gghighlight(toe_num == 250, label_key = toe_num)
q <- ggplot(lizards, aes(x = total_length, y = weight)) +
geom_line(aes(color = common_name)) +
gghighlight(max(weight) > 30)
## label_key: common_name
q
p | q #Puts graphs side by side in a row. Arrange horizonally.
(p | q) / q
p / q # puts p over q vertically
p / (p | q) &
theme_minimal()
whiptails <- lizards %>%
filter(common_name == "western whiptail") %>%
drop_na(total_length, weight)
ggplot(data = whiptails, aes(x = total_length, y = weight)) +
geom_point() +
geom_rug()
Marginal plot with boxplots
my_plot <- ggplot(data = whiptails, aes(x = total_length, y = weight)) +
geom_point(aes(color = sex), size = 2) +
scale_color_manual(values = c("cyan4", "black", "goldenrod"),
name = "Sex:",
labels = c("female", "juvenile", "male")) +
theme_minimal() +
theme(legend.position = "bottom")
ggMarginal(my_plot, type = "boxplot", groupColour = TRUE)
ggplot(data = whiptails, aes(x = sex, y = weight)) +
geom_beeswarm() +
#geom_violin(fill = NA) +
geom_boxplot(fill = NA, color = "cyan4")
Make a new dataframe called lizard_counts, starting from lizards, with date convered to class Date. Then count by year and common name the number of lizards observed.
lizard_counts2 <- lizards %>%
mutate(date = lubridate::mdy(date)) %>%
group_by(year = lubridate::year(date), common_name) %>%
summarize(counts = n())
## `summarise()` has grouped output by 'year'. You can override using the `.groups` argument.
Make the heatmap:
ggplot(data = lizard_counts2, aes(x = year, y = common_name)) +
geom_tile(aes(fill = counts)) +
geom_text(aes(label = counts), color = "white", size = 3) +
#scale_fill_gradientn(colors = c("navy", "red", "orange"))
scale_fill_viridis_c() # color blind friendly. c specifies continuous variable. d would be discrete variable.
using sf package
use read_sf to read in the “doc.kml” file
jornada_vegetation <- read_sf(here("data_raw", "spatial_vegetation", "doc.kml")) %>%
select(Name) %>%
clean_names()
ggplot(data = jornada_vegetation) +
geom_sf(aes(fill = name), color = "NA") +
scale_fill_paletteer_d("ggthemes::manyeys") +
labs(x = "Longitude",
y = "Latitude",
fill = "Dominant Vegetation") +
theme_minimal()